<?php
/**
 * 会员模型
 */
namespace app\index\model;
use think\Model;
class Member extends Model{

	//初始化方法
    protected static function init(){

    	// 添加商品后执行
        static::afterInsert(function ($user) {
        	
        	// 拼凑路径
        	$url = config('web_root').url('emaiCheck',['id'=>$user->data['mid'],'code'=>$user->data['is_validate'] ]);
        	
        	// 拼凑邮件信息
        	$msg = <<<MSG
        	<p>欢迎您成为本站会员，请点击以下链接地址完成email验证。</p>
			<p><a href="$url">点击完成验证</a></p>
MSG;
		
    		// 给用户发送验证邮件
    		SendMail($user->data['email'],'恭喜您成为了京西商城的会员！',$msg);
            
        });

        //修改商品后执行
        static::afterUpdate(function ($user) {

    		//获取商品id
    		$gid = $user->data['gid']+0;
            
        	//扩展分类处理
        	$extInput = is_array( input('post.ext_cat_id/a') ) ? array_filter( input('post.ext_cat_id/a') ) : '';
        	if( $extInput ) {
	        	$extCats = [];
	        	$gcModel = db('goods_category');

	        	//删除该商品的扩展分类
	        	$gcModel->where('gid','=',$gid)->delete();

	        	//添加扩展分类
	        	foreach ( $extInput as $v) {

	        		$extCats['gid'] = $gid;

	        		$extCats['cid'] =$v+0;

	        		$gcModel->strict('true')->insert( $extCats );

	        	}
	        }

        	//商品信息处理
        	$gInfo['seo_keyword'] = input('post.seoKeyword');
        	$gInfo['seo_desc'] = input('post.seoDescription');
        	$gInfo['content'] = input('post.content');
        	db('goods_info')->strict('true')->where('gid','=',$gid)->update( $gInfo );

        	//会员价处理
     		$mpInput = input('post.memPrices/a');
     		// q($mpInput);
     		$memPrice = [];
     		$mpModel = db('member_price');
     		$mpModel->where('gid','=',$gid)->delete();//删除该商品的扩展分类
        	foreach ($mpInput as $k => $v) {

        		//拼凑插入信息
        		$memPrice['gid'] = $gid;
	        	$memPrice['lid'] = $k;
        		$memPrice['member_price'] = $v;

        		//插入数据
        		$mpModel->strict('true')->insert( $memPrice );
        		
        	}
        	
        	//商品属性处理
        	//如果没填写商品属性就为空
        	$attrInput = is_array( input('post.attrs/a') ) ? array_filter( input('post.attrs/a') ) : '';
        	$attrModel = db('goods_attrs');//实例化模型   
        	$attr = [];//存储插入信息
        	$attrModel->where('gid','=',$gid)->delete();//删除所有商品属性
        	if( $attrInput ){//如果填写了商品属性	 

	        	//循环属性
	        	foreach ($attrInput as $k => $v) {
	        		
	        		//拼凑插入信息
	        		$attr['aid'] = $k;
	        		$attr['gid'] = $gid;

	        		//判断如果是规格
	        		if( is_array( $v ) ){
	        			$v = array_unique($v);
	        			// asort( $v,SORT_NUMERIC );
	        			usort( $v,array(__CLASS__,'str_len_sort'));

	        			//循环拼凑属性值字符串
	        			$attrValue= '';
	        			foreach ( $v as $k2 => $v2 ) {
	        				
	        				if( $v2 ) {

	        					$attrValue.=','.$v2;
	        					
	        				}
	        				
	        			}

	        			//如果属性设置了值
	        			if( $attrValue ) {

	        				//去除多余','
		        			$attr['attr_value'] = trim( $attrValue, ',') ;
		        			
		        			//插入数据库
	        				$attrModel->strict('true')->insert( $attr );
	        			}

	        		}else{
	        			
	        			//去重后如果存在数据，就插入到数据库
	        			if( $v ){

	        				$attr['attr_value'] = $v;
	        				$attrModel->strict('true')->insert( $attr );

	        			}
	        			
	        		}    	
	        		
	        	}

        	}
        	
        	//商品相册处理
            $picInput = is_array( input('post.pics/a') ) ? array_filter( input('post.pics/a') ) : '';
            $pic = [];       
            if( $picInput ) {

            	$picModel = db('pics');

	            foreach ( $picInput as $v) {

	            	$pic['gid'] = $gid;
	            	$pic['pic_big'] = $v['img_big'];
	            	$pic['pic_medium'] = $v['img_medium'];
	            	$pic['pic_small'] = $v['img_small'];
	            	$pic['sort'] = $v['sort']+0;

	            	$picModel->strict('true')->insert( $pic );
	            }

            }

        });

    }

}